package com.learn.observer.generic;


import java.util.List;
import java.util.concurrent.CopyOnWriteArrayList;

/**
 * <p>项目名称: design-pattern </p>
 * <p>文件名称: Observable </p>
 * <p>描述: TODO </p>
 *
 * @author zhousiqi
 * @version v0.0.1
 * @since 2018/10/18 17:23
 */
public abstract class Observable<S extends Observable<S,O,A>,O extends Observer<S,O,A>,A> {

    protected List<O> observers;

    public Observable() {
        this.observers = new CopyOnWriteArrayList<>();
    }

    public void addObserver(O observer) {
        this.observers.add(observer);
    }

    public void removeObserver(O observer) {
        this.observers.remove(observer);
    }
    /**
     * Notify observers
     */
    @SuppressWarnings("unchecked")
    public void notifyObservers(A argument) {
        observers.forEach(x -> x.update((S)this, argument));
    }
}
